package com.base.caselib.presenter;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.TextView;

import com.base.caselib.bean.CaseDetailBean;
import com.base.caselib.bean.CheckBoxBean;
import com.base.caselib.bean.ToDoAgreeBean;
import com.base.caselib.bean.TodoBean;
import com.base.caselib.view.CaseDetailView;
import com.base.caselib.widget.SpinnerCustom;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.RxBus2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2019/10/18.
 */

public class CaseDetailPresenter extends BasePresenter<CaseDetailView> {






    public CaseDetailPresenter(CaseDetailView view, Activity activity) {
        super(view, activity);
    }

    @Override
    public void start() {
        super.start();
    }



    public void getInfo(Map<String,String> map, final boolean isNext){
        //初始化控件
        mView.showPb_mes("正在渲染界面");
        Disposable disposable = RealNetWorkUtil.postContent()
                .content(map)
                .method("api-form/data/info")
                .build().enqueue(CaseDetailBean.class, new CallBackImpl<CaseDetailBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }

                    }
                    @Override
                    public void onNext(CaseDetailBean caseDetailBean) {
                        mView.hidePb_mes();
                        if (isNext){
                            mView.returnNextCaseDetailBean(caseDetailBean);
                        }else {
                            mView.returnCaseDetailBean(caseDetailBean);
                        }
                    }
                });
        mCompositeDisposable.add(disposable);
    }



    public void getInfo(String id){
        //初始化控件
        mView.showPb_mes("正在渲染界面");
        Map<String,String> map = new HashMap<>();
        map.put("id",id);
        Disposable disposable = RealNetWorkUtil.postContent()
                .content(map)
                .method("api-todo/todo/getInfo")
                .build().enqueue(TodoBean.RowsBean.class, new CallBackImpl<TodoBean.RowsBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }

                    }
                    @Override
                    public void onNext(TodoBean.RowsBean caseDetailBean) {
                        mView.hidePb_mes();
                        if (caseDetailBean.getParam()!=null) {
                            if (!TextUtils.isEmpty(caseDetailBean.getParam().get("lawType").toString())) {
                                mView.returnLawType(caseDetailBean.getParam().get("lawType").toString());
                            }
                        }

                    }
                });
        mCompositeDisposable.add(disposable);
    }

    //同意不同意
    public void AgreeOrDis(ToDoAgreeBean bean){

        mView.showPb_mes("正在提交数据");
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-process/lApp/add")
                .content(bean)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        mView.showMes(objects);
                        RxBus2.getInstance().post(new TodoBean());
                        mActivity.finish();
                    }
                });
        mCompositeDisposable.add(disposable);

    }
    //案件退回
    public void CaseBack(ToDoAgreeBean bean){

        mView.showPb_mes("正在提交数据");
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-process/lApp/goBack")
                .content(bean)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        mView.showMes(objects);
                        RxBus2.getInstance().post(new TodoBean());
                        mActivity.finish();
                    }
                });
        mCompositeDisposable.add(disposable);

    }
    public void getFieldValue(Map<String,String> map, final String value){
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-form/form/dropValue")
                .content(map)
                .build().enqueueForArray(CheckBoxBean.class, new CallBackImpl<List<CheckBoxBean>>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                    }

                    @Override
                    public void onNext(List<CheckBoxBean> objects) {
                        for (CheckBoxBean bean : objects){
                            if (bean.getDropValue().equals(value)){
                                mView.returnPersion(bean.getDropLabel());
                            }
                        }

                    }
                });
        mCompositeDisposable.add(disposable);
    }

    public void getFieldValue(Map<String,String> map, final String value, final TextView tv){
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-form/form/dropValue")
                .content(map)
                .build().enqueueForArray(CheckBoxBean.class, new CallBackImpl<List<CheckBoxBean>>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                    }

                    @Override
                    public void onNext(List<CheckBoxBean> objects) {
                        for (CheckBoxBean bean : objects){
                            if (bean.getDropValue().equals(value)){
                                if (TextUtils.isEmpty(tv.getText().toString())) {
                                    tv.setText(bean.getDropLabel() == null ? "" : bean.getDropLabel());
                                }else {
                                    tv.setText(tv.getText().toString().trim() +","+(bean.getDropLabel() == null ? "" : bean.getDropLabel()));
                                }
                            }
                        }

                    }
                });
        mCompositeDisposable.add(disposable);
    }
}
